package com.king.oa.util;

import java.util.Collection;
import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.king.oa.domain.Privilege;
import com.king.oa.service.PrivilegeService;

public class InitListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		//获取容器与相关的Service对象
				ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
				PrivilegeService privilegeService = (PrivilegeService) ac.getBean("privilegeServiceImpl");
				
				System.out.println("-------->>>数据初始化<<<--------");
				//准备数据: topPrivilegeList
				System.out.println("-------->>>准备数据：topPrivilegeList");
				List<Privilege> topPrivilegeList = privilegeService.findTopList();
				sce.getServletContext().setAttribute("topPrivilegeList", topPrivilegeList);
				System.out.println("-------->>>准备数据：topPrivilegeList 成功！");
				
				//准备数据： allPrivileges
				System.out.println("-------->>>准备数据：allPrivileges");
				Collection<String> allPrivileges = privilegeService.findAllPrivilegeUrl();
				sce.getServletContext().setAttribute("allPrivileges", allPrivileges);
				System.out.println("-------->>>准备数据：allPrivileges 成功！");
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		
	}

}
